package com.scalar.forecast.projectSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.projectSetup.forms.ProjectSetupForm;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.dao.UserDefinedFieldsDAO;
import com.scalar.forecast.projectSetup.ProjectSetup;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Processes the request for saving the project setup
 *
 * Created by Veeru. 
 * Date: Feb 15, 2005
 * Time: 3:12:30 PM 
 */

public class ProjectSetupConfirmCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "projectSearch" );
        boolean success = false;
        int projectId = 0;
        DBTransaction dbTransaction = null;
        HttpSession session = request.getSession ();

        ProjectSetupForm projectSetupForm = ( ProjectSetupForm )form;
        ProjectSetup projectSetup = projectSetupForm.getProjectSetup();
        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        HashMap info = new HashMap ();
        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.PREVIOUS_ERRORS, errors );
        try {
            ArrayList selectedChipsList  = projectSetup.getSelectedChipsList();
            ArrayList selectedBoardsList = projectSetup.getSelectedBoardsList();
            ArrayList selectedKitsList   = projectSetup.getSelectedKitsList();

            ArrayList selectedParts = new ArrayList();
            String selectedPart = null;

            for ( int i = 0; i < selectedChipsList.size(); i++ ) {
                selectedPart = ( (LabelValueBean)selectedChipsList.get(i)).getValue();
                selectedParts.add( selectedPart );
            }
            for ( int i = 0; i < selectedBoardsList.size(); i++ ) {
                selectedPart = ( (LabelValueBean)selectedBoardsList.get(i)).getValue();
                selectedParts.add( selectedPart );
            }
            for ( int i = 0; i < selectedKitsList.size(); i++ ) {
                selectedPart = ( (LabelValueBean)selectedKitsList.get(i)).getValue();
                selectedParts.add( selectedPart );
            }

            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();
            projectId = ProjectSetupDAO.saveProjectSetup( dbTransaction, projectSetup );
            if ( projectId < 0 )
            {
                errors.put( "Project : '"+projectSetup.getProjectName()+"' already exists", null);
            }
            else
            {
                UserDefinedFieldsDAO.saveUserDefinedFieldValues( dbTransaction, "PROJSETUP", projectId, projectSetup.getUserDefinedFeilds() );
                ProjectSetupDAO.saveProducts( dbTransaction, projectId, selectedParts );
                success = true;
            }
        }catch( Exception e ){
            e.printStackTrace();
        }
        finally {
            if ( success ) {
                try {
                    dbTransaction.commit();
                    info.put( "Project '"+projectSetup.getProjectName()+"' saved successfully", null );
                }catch ( Exception e){
                    e.printStackTrace();
                }
            }else {
                try {
                    dbTransaction.rollback();
                } catch ( Exception e ){
                    e.printStackTrace();
                }
                errors.put( "Error : Could not save Project Setup", null);
            }
        }
        return actionForward;
    }
}
